﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace Kopno
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public string imeModula { get; set; }
        public NetworkStream n { get; set; }
        public TcpClient client { get; set; }
        public static bool Work;
        public static string sekunde;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            TcpClient clientSocket = new TcpClient();
            client = clientSocket;
            try
            {
                client.Connect(IpAdresa.Text, 5001);
                n = client.GetStream();
            }
            catch
            {
                listBox1.Items.Add("Server nije pokrenut.");
                return;
            }
            listBox1.Items.Add("Uspostava veze uspjela");
            if (spojiSeKaoAdmin.IsChecked == true)
            {
                imeModula = "Administrator";
                button4.IsEnabled = true;
            }
            else
            {
                imeModula = "Kopno";
                button4.IsEnabled = false;
            }
            spojiSeKaoAdmin.IsEnabled = false;
            button1.IsEnabled = false;
            button2.IsEnabled = true;
            button3.IsEnabled = true;
            prati.IsEnabled = true;

        }
        private void ispisi()
        {

        }
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string podaci;
                byte[] outStream = new byte[10025];
                byte[] inStream = new byte[10025];
                podaci = imeModula + ";LOGOUT;$";
                outStream = System.Text.Encoding.ASCII.GetBytes(podaci);
                n.Write(outStream, 0, outStream.Length);
                n.Flush();
                n.Read(inStream, 0, 10025);
                string odgovor = System.Text.Encoding.ASCII.GetString(inStream);
                odgovor = odgovor.Substring(0, odgovor.IndexOf("$"));
                if (odgovor == "Kraj")
                {
                    client.Close();
                }
                listBox1.Items.Add("Veza sa serverom raskinuta");
            }
            catch
            {
                listBox1.Items.Add("Operacija nije uspjela");
            }
            spojiSeKaoAdmin.IsEnabled = true;
            button1.IsEnabled = true;
            button2.IsEnabled = false;
            button3.IsEnabled = false;
            button4.IsEnabled = false;
            prati.IsEnabled = false;
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            string podaci;
            byte[] outStream = new byte[10025];
            byte[] inStream = new byte[10025];
            try
            {
                if (radioButton1.IsChecked == true)
                {
                    podaci = imeModula + ";SET;" + textBox1.Text + "$";
                    outStream = System.Text.Encoding.ASCII.GetBytes(podaci);
                    listBox1.Items.Add("Poslane varijable: " + podaci);
                    n.Write(outStream, 0, outStream.Length);
                    n.Flush();
                    n.Read(inStream, 0, 10025);
                    string odgovor = System.Text.Encoding.ASCII.GetString(inStream);
                    odgovor = odgovor.Substring(0, odgovor.IndexOf("$"));

                    listBox1.Items.Add("Odgovor: " + odgovor);
                }
                if (radioButton2.IsChecked == true)
                {
                    podaci = imeModula + ";GET;" + textBox1.Text + "$";
                    outStream = System.Text.Encoding.ASCII.GetBytes(podaci);
                    n.Write(outStream, 0, outStream.Length);
                    n.Flush();
                    n.Read(inStream, 0, 10025);
                    string odgovor = System.Text.Encoding.ASCII.GetString(inStream);
                    odgovor = odgovor.Substring(0, odgovor.IndexOf("$"));
                    listBox1.Items.Add("Zatražene varijable: " + podaci);
                    listBox1.Items.Add("Odgovor: " + odgovor);
                }
            }
            catch
            {
                listBox1.Items.Add("Operacija nije uspjela");
            }
            if (DohvatiSve.IsChecked == true)
            {
                podaci = imeModula + ";ALL;DohvacajuSeSveVarijable;$";
                outStream = System.Text.Encoding.ASCII.GetBytes(podaci);
                n.Write(outStream, 0, outStream.Length);
                n.Flush();
                n.Read(inStream, 0, 10025);
                string odgovor = System.Text.Encoding.ASCII.GetString(inStream);
                odgovor = odgovor.Substring(0, odgovor.IndexOf("$"));
                if (odgovor.Length != 0)
                {
                    odgovor = odgovor.Substring(0, odgovor.LastIndexOf(";"));
                    string[] split = null;
                    split = odgovor.Split(';');
                    listBox1.Items.Add("Poslana naredba: " + podaci);
                    foreach (var item in split)
                    {
                        listBox1.Items.Add(item);
                    }
                }
                else
                {
                    listBox1.Items.Add("Nema varijabli");
                }
            }

        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            string podaci;
            byte[] outStream = new byte[10025];
            byte[] inStream = new byte[10025];


            podaci = imeModula + ";UGASISERVER;$";
            listBox1.Items.Add(podaci);
            outStream = System.Text.Encoding.ASCII.GetBytes(podaci);
            listBox1.Items.Add("Poslane varijable: " + podaci);
            n.Write(outStream, 0, outStream.Length);
            n.Flush();

        }

        private void izbrisi_Click(object sender, RoutedEventArgs e)
        {
            listBox1.Items.Clear();
        }


        private void prati_Click(object sender, RoutedEventArgs e)
        {
            //zaustaviPracenje.IsEnabled = true;
            //sekunde = sekundeText.Text;
            //Work = true;
            //string podaci;
            //byte[] outStream = new byte[10025];
            //byte[] inStream = new byte[10025];
            //int vrijeme = int.Parse(sekunde);
            //while (Work)
            //{
            //    listBox1.Items.Clear();
            //    System.Threading.Thread.Sleep(vrijeme * 1000);
            //    prati.IsEnabled = false;
            //    podaci = imeModula + ";GETLOG;DohvacaPathLogDatoteke;$";
            //    outStream = System.Text.Encoding.ASCII.GetBytes(podaci);
            //    listBox1.Items.Add("Poslana naredba: " + podaci);
            //    n.Write(outStream, 0, outStream.Length);
            //    n.Flush();
            //    try
            //    {
            //        n.Read(inStream, 0, 10025);
            //    }
            //    catch
            //    {
            //        listBox1.Items.Add("Greška sa komunikacijom");
            //    }
            //    string odgovor = System.Text.Encoding.ASCII.GetString(inStream);
            //    odgovor = odgovor.Substring(0, odgovor.IndexOf("$"));
            //    if (!odgovor.StartsWith("greska"))
            //    {
            //        string filePath = System.IO.Path.GetTempFileName();
            //        File.Copy(odgovor, filePath, true);
            //        StreamReader dat = new StreamReader(filePath);
            //        string linija;
            //        while (!dat.EndOfStream)
            //        {
            //            linija = dat.ReadLine();
            //            listBox1.Items.Add(linija);
            //        }
            //        dat.Close();
            //    }
            //    prati.IsEnabled = true;
            //    podaci = imeModula + ";GETLOG;DohvacaPathLogDatoteke;$";
            //    outStream = System.Text.Encoding.ASCII.GetBytes(podaci);
            //    listBox1.Items.Add("Poslana naredba: " + podaci);
            //    n.Write(outStream, 0, outStream.Length);
            //    n.Flush();
            //}
        }

        private void zaustaviPracenje_Click(object sender, RoutedEventArgs e)
        {
            //Work = false;
        }

        private void dohvatiIzBaze_Click(object sender, RoutedEventArgs e)
        {
            glavnaprobaEntities db = new glavnaprobaEntities();

            var res = from c in db.Podaci
                      select c;
            foreach (var item in res)
            {
                rezultatiBaza.Items.Add(item.Varijabla.TrimEnd() + ": " + item.Vrijednost.TrimEnd());
            }
            
            Podaci podatak = new Podaci();
            podatak.Varijabla = "nova";
            podatak.Vrijednost = "67";
            db.Podaci.AddObject(podatak);
            db.SaveChanges();
            db.Connection.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {


        }

        private void spremiUBazu_Click(object sender, RoutedEventArgs e)
        {
            string podaci;
            string varijabla;
            string vrijednost;
            bool varijablaPostoji = false;
            glavnaprobaEntities db = new glavnaprobaEntities(@"glavnaproba.mdf");
            byte[] outStream = new byte[10025];
            byte[] inStream = new byte[10025];
            podaci = imeModula + ";ALL;DohvacajuSeSveVarijable;$";
            outStream = System.Text.Encoding.ASCII.GetBytes(podaci);
            n.Write(outStream, 0, outStream.Length);
            n.Flush();
            n.Read(inStream, 0, 10025);
            string odgovor = System.Text.Encoding.ASCII.GetString(inStream);
            odgovor = odgovor.Substring(0, odgovor.IndexOf("$"));
            if (odgovor.Length != 0)
            {
                odgovor = odgovor.Substring(0, odgovor.LastIndexOf(";"));
                string[] split = null;
                split = odgovor.Split(';');
                listBox1.Items.Add("Poslana naredba: " + podaci);
                foreach (var item in split)
                {
                    varijabla = item.Substring(0, item.IndexOf(':'));
                    vrijednost = item.Substring(item.IndexOf(':') + 1);
                    var res = from c in db.Podaci
                              select c;
                    varijablaPostoji = false;
                    foreach (var item1 in res)
                    {
                        if (item1.Varijabla == varijabla)
                        {
                            varijablaPostoji = true;
                        }
                    }
                    if (varijablaPostoji == true)
                    {
                        Podaci podatak = db.Podaci.Single(p => p.Varijabla == varijabla);
                        podatak.Varijabla = varijabla;
                        podatak.Vrijednost = vrijednost;
                        db.SaveChanges();
                        listBox1.Items.Add(varijabla + ":" + vrijednost);
                    }
                    else
                    {
                        Podaci podatak = new Podaci();
                        podatak.Varijabla = varijabla;
                        podatak.Vrijednost = vrijednost;
                        listBox1.Items.Add(varijabla + ":" + vrijednost);
                       
                    }
                }
            }
            db.Connection.Close();
        }

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}
